package com.pet.server.ext.action.request;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.Option;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.data.RequestInfo;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

public class List extends ActionBase{


	public Integer startIndex,itemCount,user_id;
	public String queryString,state;
	public Page page;
	public List(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {
		parseToParameter(params);
		itemCount = p.itemCount==0?10:p.itemCount;
		page = new Page(p.startIndex, itemCount,p.queryString);
		state = params.getFieldValue("state");
		user_id = NumberUtil.getInteger(params.getFieldValue("user_id"));
		
	}
	
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		userService.searchRequestInfo(page, state, user_id,null);
		for(Object r:page.getList()){
              RequestInfo req = (RequestInfo)r;	
              if(BaseAttribute.diaoc.equals(req.type)){
            	  req.options=userService.queryOptionByRequestId(req.id);
           	      String isvote=userService.isVote(user.user_id, req.id);
            	  req.is_vote=BaseAttribute.LOGIC_TRUE.equals(isvote)?BaseAttribute.LOGIC_TRUE:BaseAttribute.LOGIC_FALSE;//isvote;
            	  voteCount(req);
              }else if(BaseAttribute.tiw.equals(req.type)){
            	  if(StringUtil.equals(BaseAttribute.LOGIC_TRUE, req.state)){
            		  req.answer=userService.findAnswerInfo(req.answer_id);
            	  }
              }
		}
		Result result = new Result();
        result.setPage(page,"reply_id");
	    return result;
	}

	private void voteCount(RequestInfo req){
		Integer vote_count=0;
		for(Option o:req.options){
			vote_count+=o.getTimes();
		}
		req.vote_count=vote_count;
	}


}
